	package com.erpanderp.wam.usm.view.leave;
	
	import java.io.Serializable;
	import java.util.ArrayList;
	import java.util.Date;
	import java.util.HashMap;
	import java.util.List;
	import java.util.Map;
	
	import javax.faces.application.FacesMessage;
	import javax.faces.bean.ManagedBean;
	import javax.faces.bean.ViewScoped;
	import javax.faces.context.FacesContext;
	import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;
	
	import com.erpanderp.wam.adm.model.entity.Module;
	import com.erpanderp.wam.adm.model.entity.Role;
	import com.erpanderp.wam.adm.model.service.ModuleDAO;
	import com.erpanderp.wam.adm.model.service.RoleDAO;
	import com.erpanderp.wam.usm.model.entity.leave.LeaveType;
	import com.erpanderp.wam.usm.model.entity.leave.LeaveTypeRole;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
	import com.erpanderp.wam.usm.model.service.leave.LeaveTypeDAO;
	import com.erpanderp.wam.usm.model.service.leave.LeaveTypeRoleDAO;
	import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;
	@ManagedBean
	@ViewScoped
	public class CreateLeaveTypeBean implements Serializable {
	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private LeaveType leaveType = new LeaveType();
	private WAMUser wamUser = new WAMUser();
	private Map<String, Long> rolesMap = new HashMap<String,Long>();
	private List<Role> roles = new ArrayList<Role>();
	private RoleDAO roleDAO = new RoleDAO();
	private List<Role> selectedRoles = new ArrayList<Role>();
	LeaveTypeDAO leaveTypeDAO = new LeaveTypeDAO();
	private LeaveTypeRole leaveTypeRole;
	private boolean FORM_VALID;
	//private List<String> selectedRoles = new ArrayList<String>();
	private List<Long> selectedRoleIds = new ArrayList<Long>();
	private LeaveTypeRoleDAO leaveTypeRoleDAO = new LeaveTypeRoleDAO();
	public 	CreateLeaveTypeBean(){
		System.out.println("INTIALIZING.............");
		wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
		if(wamUser != null){
		roles = roleDAO.getAllRoles();
		System.out.println("ROLES SIZE:"+roles.size());
		for(Role role : roles){
			if(!(role.getRole().equals(CommonLookup.STANDARD_ROLE_ADMINISTRATOR))){
			 rolesMap.put(role.getDisplayName(), role.getRoleId());
			}
		}
		}else{
			ValidationUtil.gotoLoginPage();
		}
	}
	
	public void saveLeaveType(ActionEvent e){
		Role role = new Role();
		validateForm();
		if(FORM_VALID){
			System.out.println("SELECTED ROLES SIZE ="+selectedRoleIds.size());
			System.out.println("SELECTED ROLE IDS ="+selectedRoleIds);
			
			
			for(Object selectedRoleId : selectedRoleIds){
				leaveTypeRole = new LeaveTypeRole();
				role = roleDAO.getRoleByRoleID(Long.parseLong(selectedRoleId.toString()));
				selectedRoles.add(role);
				System.out.println("LEAVETYPE ROLE");
				//leaveTypeRole.setLeaveType(leaveType);
				leaveTypeRole.setRole(role);
				leaveTypeRole.setCreateDate(new Date());
				leaveTypeRole.setStatus("ACTIVE");
				leaveTypeRoleDAO.createLeaveTypeRole(leaveTypeRole);
				leaveType.getLeaveTypeRoles().add(leaveTypeRole);
			}		
			
				
			
			leaveType.setCreateDate(new Date());
			leaveType.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
			leaveType.setStatus(CommonLookup.STATUS_ACTIVE);
			
		
			try{
			
				
				leaveTypeDAO.createLeaveType(leaveType);
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, leaveType.getLeaveType() + " leavetype created successfully.", ""));
				leaveType = new LeaveType();
			}catch (Exception ex) {
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Leave Type creation failed.", ""));
			}
			
			
		}else{
			
		}
		System.out.println("IN SAVE MODULE");
	}
	
	
	private void validateForm(){
		FORM_VALID = true;
		
		if(ValidationUtil.isEmpty(leaveType.getLeaveType(), "Leave Type", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(leaveType.getDisplayname(), "Display Name", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(leaveType.getDisplayOrder().toString(), "Display Order", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		/*if(ValidationUtil.isEmpty, "Display Order", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}*/
		
		
	}
	
	//GETTERS NAD SETTERS
	public LeaveType getLeaveType() {
		return leaveType;
	}
	
	public void setLeaveType(LeaveType leaveType) {
		this.leaveType = leaveType;
	}
	
	public boolean isFORM_VALID() {
		return FORM_VALID;
	}
	
	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}
	public LeaveTypeDAO getLeaveTypeDAO() {
		return leaveTypeDAO;
	}
	
	public void setLeaveTypeDAO(LeaveTypeDAO leaveTypeDAO) {
		this.leaveTypeDAO = leaveTypeDAO;
	}
	
	public Map<String, Long> getRolesMap() {
		return rolesMap;
	}
	
	public void setRolesMap(Map<String, Long> rolesMap) {
		this.rolesMap = rolesMap;
	}
	
	public List<Role> getRoles() {
		return roles;
	}
	
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	
	public List<Role> getSelectedRoles() {
		return selectedRoles;
	}
	
	public void setSelectedRoles(List<Role> selectedRoles) {
		this.selectedRoles = selectedRoles;
	}
	
	public LeaveTypeRole getLeaveTypeRole() {
		return leaveTypeRole;
	}
	
	public void setLeaveTypeRole(LeaveTypeRole leaveTypeRole) {
		this.leaveTypeRole = leaveTypeRole;
	}
	
	
	
	public List<Long> getSelectedRoleIds() {
		return selectedRoleIds;
	}
	
	public void setSelectedRoleIds(List<Long> selectedRoleIds) {
		this.selectedRoleIds = selectedRoleIds;
	}
	
	public LeaveTypeRoleDAO getLeaveTypeRoleDAO() {
		return leaveTypeRoleDAO;
	}
	
	public void setLeaveTypeRoleDAO(LeaveTypeRoleDAO leaveTypeRoleDAO) {
		this.leaveTypeRoleDAO = leaveTypeRoleDAO;
	}

	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}
	}
